package com.truongnha.clienttn.business.service;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import com.truongnha.clienttn.business.model.TeacherAttendModel;
import com.truongnha.clienttn.data.dao.CreateDatabase;
import com.truongnha.clienttn.data.dao.TeacherAttendDAO;
import com.truongnha.clienttn.data.sao.TeacherAttendSAO;

public class TeacherAttendService {
	static Context context;
	private CreateDatabase db;
	private int classId;
	private int day;
	private int month;
	private int year;

	public TeacherAttendService(Context context) {
		super();
		this.context = context;
	}

	public void getDataFromServer(int classId, int day, int month, int year) {
		TeacherAttendSAO teacherAttendSAO = new TeacherAttendSAO(context,
				classId, day, month, year);
		teacherAttendSAO.getDataFromServer();
	}

	public void getAttendFromDatabase(List<TeacherAttendModel> list) {
		try {
			db = new CreateDatabase(context);
			// get list subject
			TeacherAttendDAO teacherAttendDAO = new TeacherAttendDAO(db.open());
			String data = teacherAttendDAO.get(classId, day, month, year);
			JSONArray jsArray = new JSONArray(data);
			int size = jsArray.length();
			
			for (int i = 0; i < size; i++) {
				JSONObject js = jsArray.getJSONObject(i);
				TeacherAttendModel teacherAttendModel = new TeacherAttendModel(
						js.getString("id"), i, js.getString("lastName") + " "+ js.getString("firstName"),
						js.getString("birth"), js.getString("sex"), js.getString("status"));
				list.add(teacherAttendModel);
			}
			db.close();
		} catch (Exception e) {
			db.close();
			Log.e("log error",
					"error in teacher attend service 1" + e.toString()
							+ e.getStackTrace().toString());
		}
	}
	
	public static  void submitAttendance(TeacherAttendModel teacherAttendModel, String date) {
		TeacherAttendSAO  teacherAttendSAO = new TeacherAttendSAO(context);
		teacherAttendSAO.submitAttendance(teacherAttendModel,date);
	}
}
